import { http } from "@/utils/http";

export type UserResult = {
  code: number;
  data: {
    /** 用户名 */
    username: string;
    userId: string;
    /** 当前登陆用户的角色 */
    roles: Array<string>;
    /** `token` */
    token: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
    avatar: string;
    nickname: string;
  };
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", "/user/login", { data });
};

/** 刷新token */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refresh-token", { data });
};

/** 获取所有用户 */
export const getUserList = (params?: object) => {
  return http.request<any>("get", "/user/getUserList", { params });
};

/** 修改用户信息 */
export const updateUserInfo = (data?: object) => {
  return http.request<any>("put", "/user/updateUserInfo", { data });
};

/** 修改用户信息 */
export const saveUser = (data?: object) => {
  return http.request<any>("post", "/user/save", { data });
};

/** 删除用户 */
export const deleteUser = (params?: object) => {
  return http.request<any>("delete", "/user/deleteUser?ids=" + params, { params });
};

/** 图片上传 */
export const imageUpload = (data: FormData) => {
  return http.request<any>(
    "post",
    "/upload/image",
    { data },
    { headers: { "Content-Type": "multipart/form-data" } }
  );
};
